package io.hx.modules.rep.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import io.hx.common.validator.ValidatorUtils;
import io.hx.modules.patient.dao.PatientUserDao;
import io.hx.modules.patient.entity.FilesEntity;
import io.hx.modules.patient.service.FilesService;
import io.hx.modules.sys.dao.SysUserDao;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.hx.modules.rep.entity.DiagnosticEntity;
import io.hx.modules.rep.service.DiagnosticService;
import io.hx.common.utils.PageUtils;
import io.hx.common.utils.R;



/**
 * 诊断报告
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2023-03-21 11:28:39
 */
@RestController
@RequestMapping("rep/diagnostic")
public class DiagnosticController {
    @Autowired
    private DiagnosticService diagnosticService;
    @Autowired
    private PatientUserDao patientUserDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private FilesService filesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("rep:diagnostic:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = diagnosticService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("rep:diagnostic:info")
    public R info(@PathVariable("id") Integer id){

        DiagnosticEntity diagnostic = diagnosticService.getById(id);
        diagnostic.setSysUserEntity(sysUserDao.selectById(diagnostic.getSysUserId()));
        diagnostic.setUser(patientUserDao.selectById(diagnostic.getUserId()));
        System.out.println(diagnostic.toString());
         R.ok().put("userdata1", diagnostic.getUser());
        return R.ok().put("diagnostic", diagnostic);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("rep:diagnostic:save")
    public R save(@RequestBody DiagnosticEntity diagnostic){
        diagnostic.setUploadTime(new Date());
        diagnosticService.save(diagnostic);
        return R.ok();
    }

    /**
     * 修改
     */

    @RequestMapping("/update")
    @RequiresPermissions("rep:diagnostic:update")
    @Transactional
    public R update(@RequestBody DiagnosticEntity diagnostic){
        ValidatorUtils.validateEntity(diagnostic);
        diagnosticService.updateById(diagnostic);
        FilesEntity filesEntity = new FilesEntity();
        filesEntity.setAppointmentId(diagnostic.getAppointmentId());
        filesEntity.setUserId(diagnostic.getUserId());
            filesEntity.setNumberVisit(1);
            filesEntity.setCarteVital("12121");
            filesEntity.setCreateTime(new Date());
            filesEntity.setPatientType(0);
            filesEntity.setMedicalType(0);
            filesEntity.setAddress("贵州省");
            boolean save = filesService.save(filesEntity);
        System.out.println(save);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("rep:diagnostic:delete")
    public R delete(@RequestBody Integer[] ids){
        diagnosticService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
